package com.letv.core.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.letv.core.constant.DownloadConstant;
import com.letv.core.db.PreferencesManager;

import java.io.File;

/**
 * 下载相关的工具类
 * 
 * @author wuxinrong
 *
 */
public class DownloadUtils {
    private static final String PUSH = "push";

    public static File getDownloadDir(Context context) {
        File downloadDir = new File(getDownloadLocation(context));
        if (!downloadDir.exists()) {
            downloadDir.mkdirs();
        }
        return downloadDir;
    }

    /**
     * 得到默认下载路径
     * */
    public static String getDownloadLocation(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PUSH, Context.MODE_PRIVATE);
        return sp.getString(DownloadConstant.DOWNLOAD_LOCATION_KEY, DownloadConstant.DOWNLOAD_LOCATION_DIR);
    }

    enum State {
        ERROR, CLOSE
    }

    /**
     * 返回当前下载路径
     * 
     * @return
     */
    public static File getDownloadDir() {
        String path = PreferencesManager.getInstance().getDownloadLocation();
        if (TextUtils.isEmpty(path)) {
            path = DownloadConstant.DOWNLOAD_LOCATION_DIR;
        }

        File downloadDir = new File(path);
        if (!downloadDir.exists()) {
            downloadDir.mkdirs();
        }

        return downloadDir;
    }

    /**
     * 从当前路径获得下载文件
     * 
     * @param episodeid
     * @return
     */
    public static File getCurrentDownloadFile(long episodeid) {
        File file = null;

        if (StoreUtils.isSdcardAvailable()) {
            File dir = getDownloadDir();
            if (null != dir) {
                file = new File(dir, createFileName(episodeid));
            }
        }
        return file;
    }

    /**
     * 读取已下载的文件目录---新
     */
    public static File getDownloadFile(long episodeid) {
        if (!StoreUtils.isSdcardAvailable()) {
            return null;
        }

        File downloadDir = null;
        // DownloadDBBean info = DBManager.getInstance().getDownloadTrace().getTitleInFinish(episodeid);
        // if(info != null && info.getFilePath() != null){
        // downloadDir = new File(info.getFilePath());
        // }else{
        // /**
        // * 以前老版本，没有对应的记录，从原来默认路径获得数据
        // */
        // downloadDir = new File(Environment.getExternalStorageDirectory(),StoreUtils.DOWNLOAD);
        // }
        File downloadFile = new File(downloadDir, createFileName(episodeid));

        return downloadFile;
    }

    /**
     * 读取已下载的文件目录---旧
     * 
     * @param albumId
     * @param order
     * @return
     */
    public static File getDownloadFile(int albumId, float order) {
        if (!StoreUtils.isSdcardAvailable()) {
            return null;
        }

        File downloadDir = null;
        // DownloadDBBean info = DBManager.getInstance().getDownloadTrace().getTitleInFinish(albumId, order);
        // if(info != null && info.getFilePath() != null){
        // downloadDir = new File(info.getFilePath());
        // }else{
        // /**
        // * 以前老版本，没有对应的记录，从原来默认路径获得数据
        // */
        // downloadDir = new File(Environment.getExternalStorageDirectory(),StoreUtils.DOWNLOAD);
        // }

        File downloadFile = new File(downloadDir, createFileName(albumId, order));

        return downloadFile;
    }

    /**
     * 根据专辑ID与对应的技术构建下载文件名---旧
     * 
     * @param albumId
     * @param order
     * @return
     */
    private static String createFileName(int albumId, float order) {
        String name = albumId + "_" + order;
        return name.trim() + ".mp4";
    }

    /**
     * 根据视屏ID 构建下载文件名-----新
     * 
     * @param episodeid
     * @return
     */
    public static String createFileName(long episodeid) {
        String name = episodeid + "";
        return name.trim() + ".mp4";
    }
}
